home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Utilities / UseRsrcM.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  4.5 KB  |  243 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        UseRsrcM.cpp
  3.  
  4.     Contains:    Functions for using resources from the resource fork of a library
  5.  
  6.     Owned by:    Jens Alfke
  7.  
  8.     Copyright:    © 1994 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10. */
  11.  
  12.  
  13. #ifndef _USERSRCM_
  14. #include "UseRsrcM.h"
  15. #endif
  16.  
  17. #ifndef __TEXTUTILS__
  18. #include <TextUtils.h>
  19. #endif
  20.  
  21. #ifndef _EXCEPT_
  22. #include "Except.h"
  23. #endif
  24.  
  25. #ifndef _ODDEBUG_
  26. #include "ODDebug.h"
  27. #endif
  28.  
  29. #ifndef _ODMEMORY_
  30. #include <ODMemory.h>
  31. #endif
  32.  
  33. #ifndef _PASCLSTR_
  34. #include <PasclStr.h>
  35. #endif
  36.  
  37. #ifndef __FRAGRSRC__
  38. #include <FragRsrc.h>
  39. #endif
  40.  
  41. #ifndef __RESOURCES__
  42. #include <Resources.h>
  43. #endif
  44.  
  45. #ifndef __ERRORS__
  46. #include <Errors.h>
  47. #endif
  48.  
  49.  
  50. static CFragResFileRef    gFragRef = kODNULL;        // Reference to resource map
  51. static short            gRefNum = -1;            // RefNum of resource file
  52. static ODSLong            gNesting = 0;            // Nesting level (>0 means in use)
  53.  
  54.  
  55. OSErr
  56. InitLibraryResources( CFragInitBlockPtr init )
  57. {
  58.     return InitCFragResources(init,&gFragRef);
  59. }
  60.  
  61.  
  62. void
  63. CloseLibraryResources( )
  64. {
  65.     if( gNesting>0 )
  66.         WARN("Resource file still in use while being closed!");
  67.     OSErr err= TermCFragResources(gFragRef);
  68.     if( err )
  69.         WARN("TermCFragResources returned %hd",err);
  70.     
  71.     gFragRef = kODNULL;
  72.     gRefNum = -1;
  73.     gNesting = 0;
  74. }
  75.  
  76.  
  77. ODSLong BeginUsingLibraryResources( )
  78. {
  79.     short ref = CurResFile();
  80.     if( gNesting > 0 )
  81.         UseResFile(gRefNum);
  82.     else {
  83.         THROW_IF_ERROR( BeginCFragResources(gFragRef) );
  84.         gRefNum = CurResFile();
  85.     }
  86.     gNesting++;
  87.     return ref;
  88. }
  89.  
  90.  
  91. void EndUsingLibraryResources( ODSLong ref )
  92. {
  93.     if( gNesting > 0 )
  94.         if( --gNesting == 0 )
  95.             THROW_IF_ERROR( EndCFragResources(gFragRef) );
  96.     UseResFile(ref);
  97. }
  98.  
  99.  
  100. CUsingLibraryResources::~CUsingLibraryResources( )
  101. {
  102.     ODSLong ref = fRefNum;
  103.     if( ref != -1 ) {                        // Make sure it's done only once
  104.         fRefNum = -1;
  105.         EndUsingLibraryResources(ref);
  106.     }
  107. }
  108.  
  109.  
  110. // RESOURCE UTILITIES:
  111.  
  112.  
  113. static void*
  114. BasicReadResource( ResType type, short id, ConstStr255Param name, ODBoolean asHandle )
  115. {
  116.     Handle rsrc;
  117.     void *result = kODNULL; ODVolatile(result);
  118.     
  119.     ODSLong savedRef = BeginUsingLibraryResources();
  120.     
  121.     SetResLoad(false);
  122.     if( name )
  123.         rsrc = Get1NamedResource(type,name);
  124.     else
  125.         rsrc = Get1Resource(type,id);
  126.     SetResLoad(true);
  127.     if( rsrc==kODNULL ) {
  128.         OSErr err = ResError();
  129.         EndUsingLibraryResources(savedRef);
  130.         THROW( err ?err :resNotFound );
  131.     }
  132.     
  133.     TRY{
  134.         ODSize size = (*rsrc) ?GetHandleSize(rsrc) :GetResourceSizeOnDisk(rsrc);
  135.         void *dst;
  136.         if( asHandle ) {
  137.             result = (void*) ODNewHandle(size);
  138.             dst = ODLockHandle((ODHandle)result);
  139.         } else
  140.             result = dst = ODNewPtr(size);
  141.     
  142.         if( *rsrc == kODNULL ) {
  143.             // Resource is not in memory, use partial-resource call:
  144.             ReadPartialResource( rsrc, 0, dst, size );
  145.             THROW_IF_ERROR( ResError() );
  146.             ReleaseResource(rsrc);
  147.         } else {
  148.             // Resource is already in memory; just copy it:
  149.             ODBlockMove(*rsrc,dst,size);
  150.         }
  151.  
  152.         if( asHandle )
  153.             ODUnlockHandle((ODHandle)result);
  154.  
  155.     }CATCH_ALL{
  156.         if( *rsrc == kODNULL )
  157.             ReleaseResource(rsrc);
  158.         EndUsingLibraryResources(savedRef);
  159.         if( result )
  160.             if( asHandle )
  161.                 ODDisposeHandle((ODHandle)result);
  162.             else
  163.                 ODDisposePtr(result);
  164.         RERAISE;
  165.     }ENDTRY
  166.     
  167.     EndUsingLibraryResources(savedRef);
  168.     return result;
  169. }
  170.  
  171.  
  172. ODHandle
  173. ODReadResource( ResType type, short id )
  174. {
  175.     return (ODHandle) BasicReadResource(type,id,kODNULL,kODTrue);
  176. }
  177.  
  178. ODHandle
  179. ODReadNamedResource( ResType type, ConstStr255Param name )
  180. {
  181.     if( name==kODNULL )
  182.         THROW(kODErrIllegalNullInput);
  183.     return (ODHandle) BasicReadResource(type,0,name,kODTrue);
  184. }
  185.  
  186. ODPtr
  187. ODReadResourceToPtr( ResType type, short id )
  188. {
  189.     return BasicReadResource(type,id,kODNULL,kODFalse);
  190. }
  191.  
  192. ODPtr
  193. ODReadNamedResourceToPtr( ResType type, ConstStr255Param name )
  194. {
  195.     if( name==kODNULL )
  196.         THROW(kODErrIllegalNullInput);
  197.     return BasicReadResource(type,0,name,kODFalse);
  198. }
  199.  
  200.  
  201. void
  202. ODGetString( Str255 str, short id )
  203. {
  204.     str[0] = 0;
  205.     ODSLong savedRef = BeginUsingLibraryResources();
  206.     Handle h = Get1Resource('STR ',id);
  207.     OSErr result = ResError();
  208.     if( h ) {
  209.         CopyPascalString(str,(StringPtr)*h);
  210.         ReleaseResource(h);
  211.     }
  212.     EndUsingLibraryResources(savedRef);
  213.     if( !h ) {
  214.         THROW(result);
  215.         THROW(resNotFound);
  216.     }
  217. }
  218.  
  219.  
  220. void
  221. ODGetIndString( Str255 str, short id, short index )
  222. {
  223.     str[0] = 0;
  224.     ODSLong savedRef = BeginUsingLibraryResources();
  225.     Handle h = Get1Resource('STR#',id);
  226.     OSErr result = ResError();
  227.     if( h ) {
  228. #if ODDebug
  229.         if( index<1 || index>**(short**)h ) {
  230.             WARN("ODGetIndString(%hd,%hd): invalid index",id,index);
  231.             result= kODErrValueOutOfRange;
  232.         } else
  233. #endif
  234.         GetIndString(str,id,index);
  235.         ReleaseResource(h);
  236.     }
  237.     EndUsingLibraryResources(savedRef);
  238.     if( result ) {
  239.         THROW(result);
  240.         THROW(resNotFound);
  241.     }
  242. }
  243.